package com.bop.dehong.center;

import java.util.TreeMap;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import cn.jpush.android.api.JPushInterface;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.DeviceUuidFactory;
import com.base.dehong.util.LogUtils;
import com.base.dehong.util.PreferencesUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.center.transaction.TransactionActivity;
import com.bop.dehong.config.Config;
import com.bop.dehong.decaifu.ToubiaoTyjActivity;
import com.bop.dehong.http.model.GetUserInfoModel;
import com.bop.dehong.info.UserInfo;
import com.bop.dehong.login.LoginActivity;
import com.bop.dehong.util.WebUrlUtil;
import com.bop.dehong.webview.WebViewActivity;

/**
 * 充值
 * @author TangWenhan/YeFengyun
 */
public class RechargeActivity extends BaseActivity{
	
	EditText mMoneyEt;
	private Long local;
	private GetUserInfoModel mGetInfoModel;

	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.chongzhi);
		manager.setLeftImageVisibility(View.VISIBLE);
		manager.setRightText("充值记录");
		manager.setRightTextVisibility(View.VISIBLE);
		manager.setRightTextOnClick(this);
	}

	@Override
	protected void initUI() {
		mMoneyEt = getView(R.id.editText1);
		registerOnClickListener(getView(R.id.button1));
	}

	@Override
	protected void initData() {
		if (DeHongApp.mLoginEntity == null) {
			ToastUtils.showLong(this, "你还未登录，请先登录再投标！");
			Intent intent = new Intent(RechargeActivity.this, LoginActivity.class);
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, 100);
		}else if (DeHongApp.mUserInfo == null) {
			reqInfo();
		}
	}
	
	private void reqInfo(){
		showProgressDialog();
		if (mGetInfoModel == null) {
			mGetInfoModel = new GetUserInfoModel(this, getCallback);
		}
		local = System.currentTimeMillis() / 1000;
		mGetInfoModel.doNet(DeHongApp.mLoginEntity.uid);
	}
	
	RequestCallBack<UserInfo> getCallback = new RequestCallBack<UserInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(UserInfo info) {
			super.onSuccess(info);
			Long net = info.stamp;
			Long modify = net - local;
			if (Math.abs(modify) >= 300) {
				PreferencesUtils.putLong(RechargeActivity.this, Config.TAG, modify);	
			}
			DeHongApp.mUserInfo = info;
		}
		
	};
	
	@Override
	public void onTitleLeftClick(View v) {
//		setResult(RESULT_OK);
		super.onTitleLeftClick(v);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {
//            setResult(RESULT_OK);
            finishCurrentActivity();
        	return true;
         }
         return super.onKeyDown(keyCode, event);
     }
	
	
	public void onSubmit(){
		String money = getStr4TextView(mMoneyEt);
		if (TextUtils.isEmpty(money)) {
			ToastUtils.show(this, "请输入金额！");
			return;
		}
		Double moneyD = Double.valueOf(money);
			Double minMoney = Double.valueOf(DeHongApp.mUserInfo.sys_recharge_money);
			LogUtils.e("moneyD：" + minMoney);
			if (moneyD < minMoney) {
				ToastUtils.showLong(this, "金额必须大于起充金额" + minMoney + "元！");
				return;
			}
			Intent intent = new Intent(this,WebViewActivity.class);
			intent.putExtra("title", "充值");
			intent.putExtra("url", getUrl(money));
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, 100);
		}
		
	private String getUrl(String money){
		TreeMap<String, String> postValues = new TreeMap<String, String>();
		postValues.put("a", "injectadd");
		postValues.put("money", money);
		postValues.put("m", "User");
		postValues.put("uid", DeHongApp.mLoginEntity.uid);
		String device_code = "";
		device_code = JPushInterface.getRegistrationID(this);//传RegistrationID方便后台根据指定RegistrationID推送消息
		DeviceUuidFactory factory = new DeviceUuidFactory(this);
//		device_code = factory.getDeviceUuid() + "";
		LogUtils.e(TAG + ",device_code", "registrationId=" + device_code);
		String phone_type = factory.getPhoneType();
		postValues.put("phone_type", phone_type);
		postValues.put("device_code", device_code);
		postValues.put("platform", "android");
		return WebUrlUtil.getWebUrl(this, Config.getIP() + Config.rechager, postValues);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}
		if (requestCode == 100) {
			ToastUtils.show(this, "充值成功！");
			setResult(RESULT_OK);
			finishCurrentActivity();
		}
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.act_recharge;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.top_bar_right_txt:
			Intent intent = new Intent(this, TransactionActivity.class);
			intent.putExtra("page", 1);
			startActivity(intent);
			break;
	    case R.id.button1:
	    	onSubmit();
	    	break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}

}

